package com.dgl.sdk.MultiDownloader;

import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 涓�浇绾跨�绫�
 * @author Tibib
 *
 */
public class DownloadThread extends Thread {
	
	protected static final String TAG = "DownloadThread";
	protected File saveFile;
	protected URL downUrl;
	protected long block;
	protected int threadId = -1;	
	protected long downLength;
	protected MultiFileDownloader downloader;
	
	/**
	 * ����规�
	 * @param downloader 涓�浇��
	 * @param downUrl 涓�浇�板�
	 * @param saveFile 淇��璺��
	 * @param block 姣�釜绾跨�璐�矗涓�浇��ぇ灏�
	 * @param downLength 宸茬�涓�浇浜����
	 * @param threadId 绾跨�ID
	 */
	public DownloadThread(MultiFileDownloader downloader, URL downUrl, File saveFile, long block, long downLength, int threadId) {
		this.downUrl = downUrl;
		this.saveFile = saveFile;
		this.block = block;
		this.downloader = downloader;
		this.threadId = threadId;
		this.downLength = downLength;
	}
	
	@Override
	public void run() {
		
		RandomAccessFile threadfile = null;
		InputStream inStream = null;
		if(downLength < block){//���杞藉���
			try {
				//浣跨�Get�瑰�涓�浇
				HttpURLConnection http = (HttpURLConnection) downUrl.openConnection();
				http.setConnectTimeout(30 * 1000);
				http.setRequestMethod("GET");
				http.setRequestProperty("Accept", "video/*, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
				http.setRequestProperty("Accept-Language", "zh-CN");
				http.setRequestProperty("Referer", downUrl.toString()); 
				http.setRequestProperty("Charset", "UTF-8");
				
				long startPos = block * (threadId - 1) + downLength;//寮��浣�疆
				long endPos = block * threadId -1;//缁��浣�疆
				http.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);//璁剧疆�峰�瀹���版������
				http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
				http.setRequestProperty("Connection", "Keep-Alive");
				
				Log.i(TAG, "code:"+http.getResponseCode());
				
				
				inStream = http.getInputStream();
				byte[] buffer = new byte[1024*512];
				int offset = 0;
				threadfile = new RandomAccessFile(this.saveFile, "rwd");
				threadfile.seek(startPos);
				
				//���璇诲���熬骞朵�涓�浇�ㄥ�浜��琛����
				while (downloader.isRun()&&((offset = inStream.read(buffer)) != -1)) {
					
					Log.i(TAG, this.threadId+" offset");
					
					threadfile.write(buffer, 0, offset);
					downLength += offset;
					//璁板����涓�浇����垮害
					downloader.append(offset);
					//瀹���存�锛��搴�お���锛�
					downloader.update(this.threadId, downLength);
				}
			} catch (Exception e) { //绾跨�涓�浇杩��涓��涓��
                Message msg = new Message();
                msg.what = 2;
                msg.obj = e.getMessage();
                downloader.getHandler().sendMessage(msg);
				//���涓�浇
				downloader.setRun(false);
				
				print("Thread "+ this.threadId+ ":"+ e);
			}finally{
				
				if(inStream!=null){
					try {
						inStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
				
				if(threadfile!=null){
					try {
						threadfile.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}else{
			print("Thread " + this.threadId + " download finish");
		}
	}

	/**
	 * ����ュ�淇℃�
	 * @param msg
	 */
	protected static void print(String msg){
		Log.i(TAG, msg);
	}

}
